/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Database.DBConnection;
import Database.PropertiesManager;
import Model.Privilegio;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Maury
 */
public class PrivilegioDAO {
        private static HashMap dados;
    
    static{
        try{
            dados = new PropertiesManager("sql.properties").readPropertiesFile();
        } catch (IOException ioe){
            System.out.println(ioe);
        }
    }  
               
    public static ArrayList<Privilegio> pesquisarAll() throws SQLException, ClassNotFoundException, IOException{
        Connection conexao = DBConnection.getInstance();
        String sql = loadSQL("Select.Privilegio");
        PreparedStatement pstmt = conexao.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        ArrayList<Privilegio> privilegios = new ArrayList<Privilegio>();
        
        while(rs.next()){            
            int id = rs.getInt("ID_PRIVILEGIO");
            String nome = rs.getString("PRIVILEGIO");
            
            Privilegio privilegio = new Privilegio();
            privilegio.setId(id);
            privilegio.setNome(nome);
            privilegios.add(privilegio);
        }
        
        return privilegios;
    }
    
    public static Privilegio pesquisar(int id) throws SQLException, ClassNotFoundException, IOException{
        Connection conexao = DBConnection.getInstance();
        String sql = loadSQL("SelectById.Privilegio");
        PreparedStatement pstmt = conexao.prepareStatement(sql);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        Privilegio privilegio = null;
        
        while(rs.next()){
            String nome = rs.getString("PRIVILEGIO");            
            privilegio = new Privilegio();
            privilegio.setId(id);
            privilegio.setNome(nome);
        }
        
        return privilegio;
    }
    
    private static String loadSQL(String key){
        String sql=null;
        sql = (String) dados.get(key);
        return sql;
    }    
}
